package org.example.leetcode.core.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.example.leetcode.common.domain.SysUser;
import org.example.leetcode.common.utils.UserInfo;
import org.example.leetcode.core.mq.RabbitMQService;
import org.example.leetcode.core.mq.RabbitmqConfig;
import org.example.leetcode.core.utils.RedisUtils;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * @author topto
 */
@Component
@Slf4j
@Aspect
public class RecommendAop {
    @Resource
    RedisUtils redisUtils;
    @Resource
    RabbitMQService rabbitMQService;
    @Resource
    TaskExecutor taskExecutor;
    @Pointcut("@annotation(org.example.leetcode.core.annotation.RecommendAnnotation)")
    public void pointCut(){}

    @After("pointCut()")
    public void execute(JoinPoint joinPoint){
        SysUser userInfo = UserInfo.getUserInfo();
        if(userInfo==null){
            return;
        }
        taskExecutor.execute(()->
        {
            String key= RedisUtils.RECOMMEND_KEY + userInfo.getPhone();
            Long value = redisUtils.increment(key,60*60L);
//            if(value>1){
//                return;
//            }
            rabbitMQService.send(RabbitmqConfig.EXCHANGE,RabbitmqConfig.ROUTINGKEY_RECOMMEND,userInfo.getPhone());
            log.info("推荐mq消息发送->{}",userInfo.getPhone());
        });

    }
}
